package com.tz.farmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.controller.BusinessController;
import com.tz.farmer.mapper.FarmerKnowledgeMapper;
import com.tz.farmer.model.FarmerKnowledge;
import com.tz.farmer.service.KnowledgeService;
import com.tz.farmer.so.BaseSo;
import com.tz.farmer.so.FarmerKnowledgeSo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class KnowledgeServiceImpl extends ServiceImpl<FarmerKnowledgeMapper, FarmerKnowledge>  implements KnowledgeService {
    private static Logger logger = LoggerFactory.getLogger(KnowledgeServiceImpl.class);

    @Autowired
    FarmerKnowledgeMapper farmerKnowledgeMapper;

    @Override
    public ResultEx getKnowledgeInfo(BaseSo baseSo) throws Exception{
        //食安知识信息
        LambdaQueryWrapper<FarmerKnowledge> knowledgeQueryWrapper = Wrappers.lambdaQuery();
        knowledgeQueryWrapper.eq(FarmerKnowledge::getProperty, baseSo.getFlag()).eq(FarmerKnowledge::getIsSued,2).orderByDesc(FarmerKnowledge::getTime);
        List<FarmerKnowledge> farmerKnowledges = farmerKnowledgeMapper.selectList(knowledgeQueryWrapper);
        return ResultEx.builder().makeSuccessResult(farmerKnowledges);
    }

    @Override
    public ResultEx getKnowledgeDetailInfo(FarmerKnowledgeSo farmerKnowledgeSo) throws Exception{
        //食安知识详情信息
        LambdaQueryWrapper<FarmerKnowledge> knowledgeQueryWrapper = Wrappers.lambdaQuery();
        knowledgeQueryWrapper.eq(FarmerKnowledge::getId, farmerKnowledgeSo.getId());
        FarmerKnowledge farmerKnowledge = farmerKnowledgeMapper.selectOne(knowledgeQueryWrapper);
        //更新后台查看次数
        FarmerKnowledge knowledge=FarmerKnowledge.builder().selectNum(farmerKnowledge.getSelectNum()+1).id(farmerKnowledgeSo.getId()).build();
        farmerKnowledgeMapper.updateById(knowledge);
        logger.info("更新查看次数完成===============");
        return ResultEx.builder().makeSuccessResult(farmerKnowledge);
    }
}
